package cn.jobstart.common;

import org.apache.commons.lang3.RandomStringUtils;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class GetCode {


    private static String[] telFirst = "188,170,171,134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");

    public static String getFixLenthString(int strLength) {
        Random rm = new Random();
        // 获得随机数
        double pross = rm.nextDouble();
        // 将获得的获得随机数转化为字符串
        String fixLenthString = String.valueOf(pross);
        //	System.out.println(fixLenthString);
        // 返回固定的长度的随机数
        return fixLenthString.substring(2, strLength + 2);
    }

    public static String createRandomNumeric(int length) {

        new RandomStringUtils();
        return RandomStringUtils.randomNumeric(length);

    }

    /**
     * 生成短信验证码
     *
     * @param type
     * @return
     */
    public static String createSmsCode(int type) {

        return createRandomNumeric(2) + type + createRandomNumeric(3);
    }

    /**
     * 根据手机号码得到验证码类型
     *
     * @param smsCode
     * @return
     */
    public static int getSmsTypeByCode(String smsCode) {
        try {
            if (StringUtil.isNullOrEmpty(smsCode)) {
                return 0;
            }

            return NumberUtil.parseInt(smsCode.substring(2, 3), 0);
        } catch (Exception e) {
            return 0;
        }
    }


    /**
     * 随机生成码，0-9A-Z
     *
     * @param length 要生成的长度
     * @return
     */

    public static String createRandom(int length) {

        String order = new BigInteger(660, new Random()).toString(36).toUpperCase();
        if (length > order.length()) {
            return order;
        }
        return order.substring(0, length);

    }

    /**
     * 生成订单号
     *
     * @param length：订单长度
     * @return
     */
    public static String createOrderNo(int length) {

        String orderNo = "";

        if (length < 14) {
            orderNo = createRandomNumeric(length);
        } else if (length == 14) {
            orderNo = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        } else {
            orderNo = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + createRandomNumeric(length - 14);
        }
        return orderNo;
    }


    public static int getNum(int start, int end) {
        if (end <= start) {
            return start;
        }
        return (int) (Math.random() * (end - start + 1) + start);
    }


    public static String createOrderNoByType(int type) {
        return GetCode.createOrderNo(14) + type + GetCode.createRandomNumeric(5);
    }

    public static int getOrderTypeByOrderNumber(String orderNumber) {

        if (StringUtil.isNullOrEmpty(orderNumber) || orderNumber.length() != 26) {
            return 1;
        }
        return NumberUtil.parseInt(orderNumber.substring(20, 21), 1);

    }

    /**
     * 根据老的得到一个新的
     *
     * @param orderNum
     * @return
     */
    public static String getNewCreateOrderByOld(String orderNum) {
        try {
            return orderNum + "-" + createRandomNumeric(5);
        } catch (Exception e) {

        }
        return orderNum;
    }

    /**
     * 根据老的得到一个新的
     *
     * @param orderNum
     * @return
     */
    public static String getOldCreateOrderByNew(String orderNum) {

        try {
            return orderNum.split("-")[0];
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderNum;
    }


    /**
     * 转数字转字符，不足位补0
     *
     * @param max
     * @param strMaxLen
     * @return
     */
    public static String intToStr(int max, int strMaxLen) {


        return String.format("%0" + strMaxLen + "d", max);

    }


}